package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.Admin;
import cn.edu.ujn.shi.dao.LoginLog;
import cn.edu.ujn.shi.dao.Msg;
import cn.edu.ujn.shi.dao.User;
import cn.edu.ujn.shi.service.*;
import cn.hutool.core.date.DateUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletResponse;


import jakarta.servlet.http.HttpSession;

import java.io.PrintWriter;
import java.util.List;

/**
 * 静态页面跳转控制
 */
@Controller
public class BaseController {
    @Autowired
    IUserService userService;

    @Autowired
    IAdminService iAdminService;

    @Autowired
    IMessageService messageService;

    @Autowired
    ILoginLogService loginLogService;

    @Autowired
    IUserTypeService iUserTypeService;

    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        return "login";
    }

    @RequestMapping("/AddMsg")
    public String AddMsg(HttpServletRequest request, HttpServletResponse response) {
        return "AddMsg";
    }

    @RequestMapping("/AdminLoginView")
    public String AdminLoginView(HttpServletRequest request, HttpServletResponse response) {
        return "AdminLoginView";
    }

    @RequestMapping("/AddAdmin")
    public String AddAdmin(HttpServletRequest request, HttpServletResponse response) {
        //管理员修改和增加页面
        request.setAttribute("username", request.getParameter("username"));
        request.setAttribute("userpass", request.getParameter("userpass"));
        return "AddAdmin";
    }

    @RequestMapping("/Register")
    public String Register(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("type", iUserTypeService.getAll());
        return "Register";
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "index";
    }

    //登录方法
    @RequestMapping("/LoginServlet")
    public void loginServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            String username = request.getParameter("username");
            String userpass = request.getParameter("userpass");

            String power = request.getParameter("power");

            //用户名存到session里面
            HttpSession hs = request.getSession();
            hs.setAttribute("username", username);

            PrintWriter out = response.getWriter();   //做输出流

            //判断是用户权限登录还是管理员权限登录
            if (power.equals("用户")) {
                List<User> userlist = userService.getRepeat(new User(username, userpass));
                if (userlist.isEmpty()) {
                    out.println("<script language = javascript>alert('用户名或密码错误')");
                    out.println("window.history.go(-1)</script>");
                    loginLogService.insert(new LoginLog(username, DateUtil.now(), "登录失败", "用户名或密码错误", "用户"));
                } else {
                    loginLogService.insert(new LoginLog(username, DateUtil.now(), "登录成功", "", "用户"));

                    response.sendRedirect("MainServlet");
                }
            } else {
                List<Admin> userlist = iAdminService.getRepeat(new Admin(username, userpass));
                if (userlist.isEmpty()) {
                    out.println("<script language = javascript>alert('管理员登录失败')");
                    out.println("window.history.go(-1)</script>");
                    loginLogService.insert(new LoginLog(username, DateUtil.now(), "登录失败", "管理员登录失败", "管理员"));

                } else {
                    loginLogService.insert(new LoginLog(username, DateUtil.now(), "登录成功", "", "管理员"));
                    response.sendRedirect("AdminLoginView");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/MainServlet1")
    public String MainServlet1() {
        return "MainServlet1";

    }

    /**
     * 渲染首页
     *
     * @param request
     * @param response
     */
    @RequestMapping("/MainServlet")
    public String mainServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            String where = request.getParameter("where");
            List<Msg> list = messageService.getSearch(where);
            request.setAttribute("list", list);
            return "MainServlet1";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}

